package com.snda.lib.util;

import android.content.Context;
import android.content.SharedPreferences;

public class PrefHelper {

	/**
	 * 在Pref中写入字符串
	 * 
	 * @param context
	 * @param name
	 * @param val
	 * @return
	 */
	public static boolean setPreferString(Context context, String strPrefName,
			String strKeyName, String strKeyValue) {
		if (context == null || strPrefName == null || strKeyName == null)
			return false;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		if (strKeyValue == null) {
			editor.remove(strKeyName);
		} else {
			editor.putString(strKeyName, strKeyValue);
		}
		return editor.commit();
	}

	/**
	 * 从Pref中读取字符串
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static String getPreferString(Context context, String strPrefName,
			String strKeyName) {
		if (context == null || strPrefName == null || strKeyName == null)
			return "";

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		return settings.getString(strKeyName, "");
	}

	/**
	 * 在Pref中写入整数
	 * 
	 * @param context
	 * @param name
	 * @param val
	 * @return
	 */
	public static boolean setPreferInt(Context context, String strPrefName,
			String strKeyName, Integer nKeyValue) {
		if (context == null || strPrefName == null || strKeyName == null)
			return false;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		if (nKeyValue == null) {
			editor.remove(strKeyName);
		} else {
			editor.putInt(strKeyName, nKeyValue);
		}
		return editor.commit();
	}

	/**
	 * 从Pref中读取整数
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static Integer getPreferInt(Context context, String strPrefName,
			String strKeyName) {
		if (context == null || strPrefName == null || strKeyName == null)
			return 0;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		return settings.getInt(strKeyName, 0);
	}

	/**
	 * 在Pref中写入整数
	 * 
	 * @param context
	 * @param name
	 * @param val
	 * @return
	 */
	public static boolean setPreferLong(Context context, String strPrefName,
			String strKeyName, Long nKeyValue) {
		if (context == null || strPrefName == null || strKeyName == null)
			return false;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		if (nKeyValue == null) {
			editor.remove(strKeyName);
		} else {
			editor.putLong(strKeyName, nKeyValue);
		}
		return editor.commit();
	}

	/**
	 * 从Pref中读取整数
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static Long getPreferLong(Context context, String strPrefName,
			String strKeyName) {
		if (context == null || strPrefName == null || strKeyName == null)
			return (long) 0;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		return settings.getLong(strKeyName, 0);
	}

	/**
	 * 在Pref中写入布尔值
	 * 
	 * @param context
	 * 
	 * @param name
	 * 
	 * @param val
	 * 
	 * @return
	 */
	public static boolean setPreferBoolean(Context context, String strPrefName,
			String strKeyName, Boolean bKeyValue) {
		if (context == null || strPrefName == null || strKeyName == null)
			return false;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		if (bKeyValue == null) {
			editor.remove(strKeyName);
		} else {
			editor.putBoolean(strKeyName, bKeyValue);
		}
		return editor.commit();
	}

	/**
	 * 从Pref中读取整数
	 * 
	 * @param context
	 * @param name
	 * @return
	 */
	public static Boolean getPreferBoolean(Context context, String strPrefName,
			String strKeyName) {
		if (context == null || strPrefName == null || strKeyName == null)
			return false;

		SharedPreferences settings = context.getSharedPreferences(strPrefName,
				Context.MODE_PRIVATE);
		return settings.getBoolean(strKeyName, false);
	}
}